package com.gwtgeo.ui.client.mvp.windowsmanager;

import net.customware.gwt.presenter.client.EventBus;

import com.google.inject.Inject;
import com.gwtgeo.ui.shared.event.CloseWinModalEvent;
import com.gwtgeo.ui.shared.event.CloseWinModalEventHandler;
import com.gwtgeo.ui.shared.event.HideViewEvent;
import com.gwtgeo.ui.shared.event.HideViewEventHandler;
import com.gwtgeo.ui.shared.event.OpenWinModalEvent;
import com.gwtgeo.ui.shared.event.OpenWinModalEventHandler;
import com.gwtgeo.ui.shared.event.RegisterViewEvent;
import com.gwtgeo.ui.shared.event.RegisterViewEventHandler;
import com.gwtgeo.ui.shared.event.ShowViewEvent;
import com.gwtgeo.ui.shared.event.ShowViewEventHandler;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Window;

public class WindowsManager {

	private WindowsProvider windowsProvider;

	private EventBus eventBus;

	private Canvas container;

	@Inject
	public WindowsManager(WindowsProvider windowdsProvider, EventBus eventBus) {
		super();
		this.windowsProvider = windowdsProvider;
		this.eventBus = eventBus;
	}

	public void bind() {

		eventBus.addHandler(RegisterViewEvent.TYPE,
				new RegisterViewEventHandler() {

					@Override
					public void onRegisterView(RegisterViewEvent event) {
						WindowedPresenter<? extends WindowedDisplay> windowsPresenter = windowsProvider
								.getWindow(event.getType());
						if (!windowsPresenter.isBound()) {
							windowsPresenter.bind();

							Window window = windowsPresenter.getDisplay()
									.asWindow();
							window.setVisible(false);
							container.addChild(window);
							window.setKeepInParentRect(true);
						}
					}
				});

		eventBus.addHandler(ShowViewEvent.TYPE, new ShowViewEventHandler() {

			@Override
			public void onShowView(ShowViewEvent event) {
				WindowedPresenter<? extends WindowedDisplay> windowsPresenter = windowsProvider
						.getWindow(event.getType());
				windowsPresenter.show();
			}
		});

		eventBus.addHandler(HideViewEvent.TYPE, new HideViewEventHandler() {

			@Override
			public void onHideView(HideViewEvent event) {
				WindowedPresenter<? extends WindowedDisplay> windowsPresenter = windowsProvider
						.getWindow(event.getType());
				windowsPresenter.hide();
			}
		});

		eventBus.addHandler(OpenWinModalEvent.TYPE,
				new OpenWinModalEventHandler() {

					@Override
					public void onOpenWinModal(OpenWinModalEvent event) {
						WindowedPresenter<? extends WindowedDisplay> windowsPresenter = windowsProvider
								.getWindow(event.getType());
						if (!windowsPresenter.isBound()) {
							windowsPresenter.bind();
							Window window = windowsPresenter.getDisplay()
									.asWindow();
							window.setKeepInParentRect(true);
							window.setIsModal(true);
							windowsPresenter.show();
						}
					}
				});

		eventBus.addHandler(CloseWinModalEvent.TYPE,
				new CloseWinModalEventHandler() {

					@Override
					public void onCloseWinModal(CloseWinModalEvent event) {
						WindowedPresenter<? extends WindowedDisplay> windowsPresenter = windowsProvider
								.getWindow(event.getType());
						if (windowsPresenter.isBound()) {
							windowsPresenter.hide();
							windowsPresenter.unbind();
							if (event.getReceptors() != null) {
								for (WindowTypeEnum win : event.getReceptors()) {
									WindowedPresenter<? extends WindowedDisplay> receptor = windowsProvider
											.getWindow(win);
									receptor.retrieveWinData(event.getType(),
											event.getData());
								}
							}
						}
					}
				});
	}

	public void setContainer(Canvas container) {
		this.container = container;
	}
}
